package Persistence;

import java.sql.ResultSet;
import java.sql.SQLException;

import Dominion.*;

public class ArticleReviewDAO extends CrudDAO<ArticleReview>{

    @Override
    public ArticleReview create(ArticleReview artrev) {
        String sqlQuerry = "INSERT INTO ArticleReview (idArticle, reviewer_mail, comment, review_date, state_review, mark) VALUES ('"
        		+artrev.getIdArticle()+"','"+artrev.getReviewer_mail()+"','"+artrev.getComment()+"','"
        		+artrev.getReview_date()+"','"+artrev.getState_review()
        		+"','"+artrev.getMark()+"')";
        try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return null;
    }

    @Override
    public void delete(ArticleReview artrev) {
    	 try {
 			AgenteBD.getDbCon().queryUpdate("DELETE FROM ArticleReview WHERE idArticle='"+artrev.getIdArticle()+"' and reviewer_mail='"+artrev.getReviewer_mail()+"'");
 		} catch (SQLException e) {
 			e.printStackTrace();
 		}
    }

    @Override
    public ArticleReview read(ArticleReview artrev) throws SQLException {
      ResultSet rs = AgenteBD.getDbCon().query(
                "SELECT * FROM ArticleReview WHERE idArticle='"+artrev.getIdArticle()+"' and reviewer_mail='"+artrev.getReviewer_mail()+"'");
        if (rs != null) {
            try {
                while (rs.next()) {
                    artrev.setComment(rs.getString("comment"));
                    artrev.setReview_date(rs.getString("review_date"));
                    artrev.setState_review(rs.getString("state_review"));
                    artrev.setMark(rs.getInt("mark"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        rs.close();
        return artrev;
    }

    @Override
    public void update(ArticleReview artrev) {
    	 String sqlQuerry="UPDATE ArticleReview SET comment='"+artrev.getComment()
		         +"', review_date='" + artrev.getReview_date()
		         +"', state_review='" +artrev.getState_review()
		         +"', mark='"+artrev.getMark()
		         + "' WHERE idArticle='"+artrev.getIdArticle()+"' and reviewer_mail='"+artrev.getReviewer_mail()+"'";
		try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}       
    }
}
